import { Navigate, BrowserRouter as Router, useRoutes } from "react-router-dom";
import autoload from "./autoload";
import Login from "@/pages/Login";
import React from "react";
import Home from "@/pages/Home";
import App from "@/App";
import AppRoute from "@/components/AppRoute";

const AppLogin = ({ children }: any) => {
    const token = localStorage.getItem('token');
    return token ? <Navigate to="/" replace /> : children;
}

const routes = [
    {
        path: '/login',
        element: <AppLogin><Login /></AppLogin>,
    },
    {
        path: '/',
        element: <App />,
        redreict: '/',
        children: [
            {
                path: '/',
                element: <AppRoute><Home /></AppRoute>
            }
        ]
    },
    {
        path: '*',
        element: <div>404</div>
    }
];

const SetRoutes = () => {
    routes[1].children = [
        ...routes[1].children as any,
        ...autoload()
    ]
    return useRoutes(routes);
}

const Routers = (props: any) => {
    return (
        <Router>
            {/*  */}
            <SetRoutes />
        </Router>
    );
};

// 新的路由出现时更新
export default React.memo(Routers);